from django.db import models


class City(models.Model):
    """城市模型"""
    name = models.CharField(max_length=100, unique=True, verbose_name="城市名称")
    code = models.CharField(max_length=50, unique=True, verbose_name="城市代码")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "城市"
        verbose_name_plural = "城市"


class Datacenter(models.Model):
    """机房模型"""
    name = models.CharField(max_length=100, verbose_name="机房名称")
    code = models.CharField(max_length=50, unique=True, verbose_name="机房代码")
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="datacenters", verbose_name="所在城市")
    address = models.TextField(verbose_name="详细地址", blank=True)

    def __str__(self):
        return f"{self.city.name} - {self.name}"

    class Meta:
        verbose_name = "机房"
        verbose_name_plural = "机房"
        unique_together = ('name', 'city')  # 同一城市中机房名称唯一


class Host(models.Model):
    """主机模型"""
    STATUS_CHOICES = (
        ('online', '在线'),
        ('offline', '离线'),
        ('maintenance', '维护中'),
    )

    hostname = models.CharField(max_length=100, unique=True, verbose_name="主机名")
    ip_address = models.CharField(max_length=50, unique=True, verbose_name="IP地址")
    datacenter = models.ForeignKey(Datacenter, on_delete=models.CASCADE, related_name="hosts", verbose_name="所在机房")
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='online', verbose_name="状态")
    cpu_cores = models.PositiveIntegerField(default=1, verbose_name="CPU核心数")
    memory_gb = models.PositiveIntegerField(default=1, verbose_name="内存大小(GB)")
    disk_gb = models.PositiveIntegerField(default=0, verbose_name="硬盘大小(GB)")
    os_type = models.CharField(max_length=50, verbose_name="操作系统类型", blank=True)
    ssh_port = models.PositiveIntegerField(default=22, verbose_name="SSH端口")
    root_password = models.CharField(max_length=128, verbose_name="Root密码")
    notes = models.TextField(blank=True, verbose_name="备注")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    is_active = models.BooleanField(default=True, verbose_name="是否激活")

    def __str__(self):
        return f"{self.hostname} ({self.ip_address})"

    class Meta:
        verbose_name = "主机"
        verbose_name_plural = "主机"


class PasswordHistory(models.Model):
    """密码历史记录"""
    host = models.ForeignKey(Host, on_delete=models.CASCADE, related_name="password_history", verbose_name="主机")
    old_password = models.CharField(max_length=128, verbose_name="旧密码")
    new_password = models.CharField(max_length=128, verbose_name="新密码")
    changed_at = models.DateTimeField(auto_now_add=True, verbose_name="修改时间")
    changed_by = models.CharField(max_length=50, default="system", verbose_name="修改人")

    def __str__(self):
        return f"{self.host.hostname} - {self.changed_at}"

    class Meta:
        verbose_name = "密码历史"
        verbose_name_plural = "密码历史"
        ordering = ['-changed_at']


class HostStatistics(models.Model):
    """主机统计数据"""
    date = models.DateField(verbose_name="统计日期")
    city = models.ForeignKey(City, on_delete=models.CASCADE, related_name="statistics", verbose_name="城市")
    datacenter = models.ForeignKey(Datacenter, on_delete=models.CASCADE, related_name="statistics", verbose_name="机房")
    host_count = models.PositiveIntegerField(default=0, verbose_name="主机数量")
    online_count = models.PositiveIntegerField(default=0, verbose_name="在线主机数")
    offline_count = models.PositiveIntegerField(default=0, verbose_name="离线主机数")
    maintenance_count = models.PositiveIntegerField(default=0, verbose_name="维护中主机数")

    def __str__(self):
        return f"{self.date} - {self.city.name} - {self.datacenter.name}"

    class Meta:
        verbose_name = "主机统计"
        verbose_name_plural = "主机统计"
        unique_together = ('date', 'city', 'datacenter')